const jwt = require('jsonwebtoken');
const helper = require('../../util/helper');
const config = require('../../config/environment/index');
const mysql = require('../../core/mysql');

const log = require('../../core/logger').getLogger("system");
const moment = require('moment');
const common = require('../../core/common');
const stringUtils = require('../../core/util/StringUtils');
const _ = require('lodash');


exports.add = async(ctx) => {
    let userName = ctx.request.body.user_name || '';
    let password = ctx.request.body.password || '';
    let avatar = ctx.request.body.avatar || '';
    let role_id = ctx.request.body.role_id || '';
    let phone = ctx.request.body.phone || '';
    let email = ctx.request.body.email || '';
    let creat_time = new Date();

    const sql = "insert manager(manager_name, hash_password,salt,accessToken,avatar,role_id,phone,email, create_time) values (?,?,?,?,?,?,?,?,?)";
    let salt = helper.makeSalt();
    var password1 = stringUtils.createPassword(password.trim());
    ret = await mysql.query(sql, [userName, password1, salt, '', '', 0, phone, email, creat_time]);
    await common.saveOperateLog(req, "新增用户：" + userName);
    ctx.body = {
        success: 1,
        userName: userName,
        message: "新增用户：" + userName
    };
}


exports.signOut = async(ctx) => {
    ctx.session.manager = null;
    ctx.body = {
        success: 1,
        message: ''
    };
}




